<template>
	<view class="list_box">
		<view class="left">
			<scroll-view scroll-y="true" :style="{ 'height':scrollHeight }">
				<view class="item" v-for="(item,index) in category" :key="index" :class="{ 'active':index==leftIndex }" :data-index="index"
				 @tap="leftTap">
					<text :style="{'font-size':'28rpx'}">{{item.name}}</text>
				</view>
			</scroll-view>
		</view>
		<view class="main">
			<swiper @change="swiperChange" class="swiper" :style="{ 'height':scrollHeight }" :current="leftIndex" vertical="true"
			 duration="300">
				<swiper-item v-for="(item,index) in category" :key="index">
					<scroll-view @scrolltolower="next" @scrolltoupper="previous" scroll-y="true" :style="{ 'height':scrollHeight }">
						<!-- <view v-for="(mainArrayItem,index1) in leftArray[index].mainArray" :key="index1" :class="{ 'active':index1==2 ,'item':true}">
							<text :style="{'font-size':'22rpx'}">
								{{mainArrayItem.name}}
							</text>
						</view> -->
						<view v-for="(item,i) in category[index].goods.list" :key="i" style="margin: 30upx;border-bottom: 1upx solid #EEEEEE;">
							<image style="width: 100%;height: 350upx;" :src="item.picUrl"></image>
							<view style="display: flex;align-items: center;">
								<text style="font-weight: bold;margin-right: 20upx;">{{item.name}}</text>
								<view style="padding-left: 10upx;border-left: 1upx solid #EEEEEE;">
									<image style="width: 40upx;height: 40upx;" src="/static/tabbar/mine02.png"></image>
								</view>
							</view>
							<view style="font-size: 20upx;">
								<text style="background-color: #FFF6F4;color: #FF795D;">预计{{item.deliveryTime}}配送</text>
								<text style="float: right;">已抢<text style="color: #FF387B;">{{item.sales}}</text>件</text>
							</view>
							<view style="display: flex;flex-direction: row;justify-content: space-between;">
								<view>
									<text style="font-size: 45upx;color: #FB206A;font-weight: bold;">￥{{item.favorablePrice}}</text>
									<text style="font-size: 25upx;color: #BBBBBB;">￥{{item.retailPrice}}</text>
								</view>
								<view style="font-weight: bold;display: flex;align-items: center;">
									<image @tap="subtract(item)" style="width: 45upx;height: 45upx;" :src="select[item.goodsId] > 0 ? '/static/subtract.png' : '/static/subtract01.png'"></image>
									<text style="margin: 20upx;font-size: 35upx;">{{select[item.goodsId] ? select[item.goodsId].count : 0}}</text>
									<image @tap="add(item)" style="width: 45upx;height: 45upx;" src="/static/add.png"></image>
								</view>
							</view>
						</view>
					</scroll-view>
				</swiper-item>
			</swiper>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'bookCategory',
		data() {
			return {
				scrollHeight: 500,
				category: [],
				leftArray: [{
						name: '全部',
						mainArray: [{
								id: 123,
								name: "设计心理学"
							},
							{
								id: 123,
								name: "色彩心理学"
							},
							{
								id: 123,
								name: "环境心理学"
							},
							{
								id: 123,
								name: "研究生/本科/专科教材"
							}
						]
					},
					{
						name: '政治',
						mainArray: [{
								id: 123,
								name: "设计心理学1"
							},
							{
								id: 123,
								name: "设计心理学"
							}
						]
					},
					{
						name: '文学鉴赏',
						mainArray: [{
								id: 123,
								name: "设计心理学2"
							},
							{
								id: 123,
								name: "设计心理学"
							}
						]
					},
				],

				leftIndex: 0,
				select: this.shopCart
			}
		},
		created() {
			/* 设置当前滚动容器的高，若非窗口的高度，请自行修改 */
			uni.getSystemInfo({
				success: (res) => {
					this.scrollHeight = `${res.windowHeight}px`;
				}
			});
		},
		onShow() {
			var index = uni.getStorageSync('categoryIndex')
			this.leftIndex = index ? index : 0
		},
		onLoad(e) {
			this.getCategory()
		},
		onTabItemTap() {
			this.select = this.shopCart
			this.$forceUpdate()
		},
		methods: {
			add(goods) {
				this.addCart(goods)
				this.select = this.shopCart
				this.$forceUpdate()
			},
			subtract(goods) {
				this.subtractCart(goods)
				this.select = this.shopCart
				this.$forceUpdate()
			},
			getCategory() {
				uni.request({
					url: this.apiUrl + "/client/category/list",
					method: "POST",
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					success: (res) => {
						const result = res.data;
						// console.log(result)
						if (result.code === 200) {
							var category = result.data.list;
							for (var i = 0; i < category.length; i += 10) {
								category[i]['goods'] = {
									noData: true,
									pageNum: 1,
									pageSize: 10,
									list: []
								}
							}
							this.category = category;
							this.getGoods()
						} else {
							uni.showToast({
								title: result.msg,
								icon: "none"
							});
						}
					},
					fail: (res) => {
						uni.showToast({
							title: res.errMsg,
							icon: "none"
						});
					}
				})
			},
			getGoods() {
				uni.request({
					url: this.apiUrl + "/client/goods/list",
					method: "POST",
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						// teamId: this.current_team.teamId,
						categoryId: this.category[this.leftIndex].categoryId,
						pageNum: this.category[this.leftIndex].goods.pageNum,
						pageSize: this.category[this.leftIndex].goods.pageSize
					},
					success: (res) => {
						const result = res.data;
						console.log(result)
						if (result.code === 200) {
							const data = result.data.list;

							if (this.category[this.leftIndex].goods.pageNum == 1) {
								this.category[this.leftIndex].goods.list = data;
							} else {
								for (var i = 0; i < data.length; i++) {
									this.category[this.leftIndex].goods.list.push(data[i])
								}
							}
							this.category[this.leftIndex].goods.pageNum = result.data.pageNum;

							// for (var i = 0; i < data.length; i++) {
							// 	this.select[data[i].goodsId] = 0
							// }

							// console.log(1111, this.select)
						} else {
							uni.showToast({
								title: result.msg,
								icon: "none"
							});
						}
					},
					fail: (res) => {
						uni.showToast({
							title: res.errMsg,
							icon: "none"
						});
					}
				})
			},

			/* 左侧导航点击 */
			leftTap(e) {
				let index = e.currentTarget.dataset.index;
				this.leftIndex = Number(index);
			},
			swiperChange(e) {
				console.log(e)
				this.leftIndex = e.detail.current
				uni.setStorageSync('categoryIndex',this.leftIndex)
				if (this.category[this.leftIndex].goods.list.length == 0) {
					this.getGoods()
				}
				
			},
			next() {
				if (this.leftIndex < this.category.length - 1) {
					this.leftIndex++
				}
			},
			previous() {
				if (this.leftIndex > 0) {
					this.leftIndex--
				}
			}
		}
	}
</script>

<style lang="scss">
	.list_box {
		display: flex;
		flex-direction: row;
		flex-wrap: nowrap;
		justify-content: flex-start;
		align-items: flex-start;
		align-content: flex-start;
		font-size: 26rpx;

		.left {
			width: 180rpx;
			// background-color: #F6FAFB;
			background-color: white;
			box-sizing: border-box;
			font-size: 26rpx;
			text-align: center;
			color: #838383;
			border-right: 1upx solid #EEEEEE;

			.item {
				position: relative;
				height: 100rpx;

				text {
					display: block;
					width: 120rpx;
					text-align: center;
					position: relative;
					margin: 0 auto;
					top: 50%;
					transform: translateY(-50%);
				}

				&.active,
				&:active {
					color: #FF387B;
					background-color: #fff;
					font-weight: bold;

					&::after {
						content: '';
						display: block;
						height: 72rpx;
						width: 8rpx;
						background: linear-gradient(90deg, rgba(255, 56, 123, 1) 0%, rgba(255, 56, 123, 1) 100%);
						border-radius: 6rpx;
						position: absolute;
						top: 14rpx;
						left: 0;
						transform: scaleY(0.5);
					}
				}
			}
		}

		.main {
			background-color: #fff;
			padding-left: 5rpx;
			width: 0;
			flex-grow: 1;
			box-sizing: border-box;

			.swiper {
				// height: 500px;
			}

			.title {
				line-height: 64rpx;
				font-size: 24rpx;
				font-weight: bold;
				color: #666;
				background-color: #fff;
				position: sticky;
				top: 0;
				z-index: 999;
			}

			.item {
				width: 156rpx;
				height: 100rpx;
				background: rgba(255, 255, 255, 1);
				border-radius: 8rpx;
				border: 1px solid rgba(171, 179, 180, 1);
				float: left;
				margin-right: 22rpx;
				margin-bottom: 22rpx;
				color: #2C343F;

				text {
					display: block;
					width: 120rpx;
					text-align: center;
					position: relative;
					margin: 0 auto;
					top: 50%;
					transform: translateY(-50%);
				}

				&.active,
				&:active {
					color: #FF387B;
					border-color: #FF387B;
				}
			}

			.goods {
				display: flex;
				flex-direction: row;
				flex-wrap: nowrap;
				justify-content: flex-start;
				align-items: center;
				align-content: center;
				margin-bottom: 10rpx;

				&>image {
					width: 120rpx;
					height: 120rpx;
					margin-right: 16rpx;
				}

				.describe {
					font-size: 24rpx;
					color: #999;
				}

				.money {
					font-size: 24rpx;
					color: #efba21;
				}
			}
		}
	}
</style>
